package drds.binlog.event;

import drds.binlog.LogByteBlock;
import drds.binlog.LogEvent;


public class XaPrepareLogEvent extends LogEvent
{

    private boolean onePhase;
    private int formatId;
    private int gtridLength;
    private int bqualLength;
    private byte[] data;

    public XaPrepareLogEvent(LogHeader logHeader, LogByteBlock buffer, FormatDescriptionLogEvent descriptionEvent)
    {
        super(logHeader);

        final int commonHeaderLength = descriptionEvent.getCommonHeaderLength();
        final int postHeaderLength = descriptionEvent.getPostHeaderLengths()[logHeader.getType() - 1];

        int offset = commonHeaderLength + postHeaderLength;
        buffer.index(offset);

        onePhase = (buffer.getInt8BasedOnIndex() == 0x00 ? false : true);

        formatId = buffer.getInt32BasedOnIndex();
        gtridLength = buffer.getInt32BasedOnIndex();
        bqualLength = buffer.getInt32BasedOnIndex();

        int MY_XIDDATASIZE = 128;
        if (MY_XIDDATASIZE >= gtridLength + bqualLength && gtridLength >= 0 && gtridLength <= 64 && bqualLength >= 0
                && bqualLength <= 64)
        {
            data = buffer.getData(gtridLength + bqualLength);
        } else
        {
            formatId = -1;
            gtridLength = 0;
            bqualLength = 0;
        }
    }

    public boolean isOnePhase()
    {
        return onePhase;
    }

    public int getFormatId()
    {
        return formatId;
    }

    public int getGtridLength()
    {
        return gtridLength;
    }

    public int getBqualLength()
    {
        return bqualLength;
    }

    public byte[] getData()
    {
        return data;
    }

}
